package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.DoctorGroupBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-09-25.
 * Description:医生分组横向选择列表
 */
public class GroupHorizontalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<DoctorGroupBean> datas = new ArrayList<>();
    private Context mContext;
    private LayoutInflater mInflater;
    private onGroupHorizontalClickListener listener;
    private int itemWidth;

    public GroupHorizontalAdapter(Context mContext, List<DoctorGroupBean> datas, onGroupHorizontalClickListener listener, int screenWidth) {
        this.mContext = mContext;
        this.datas = datas;
        mInflater = LayoutInflater.from(mContext);
        this.listener = listener;
        this.itemWidth = screenWidth / 8;
    }

    public void setDatas(List<DoctorGroupBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_grouphorizontal, null);
        groupHorizontalHolder holder = new groupHorizontalHolder(view);

        RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
        holder.tv_group.setLayoutParams(lp_tv);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        groupHorizontalHolder mHolder = (groupHorizontalHolder) holder;
        DoctorGroupBean group = datas.get(position);

        if (group.isSelect()) {
            mHolder.tv_group.setBackgroundResource(R.drawable.circle_blue_easy);
        } else {
            mHolder.tv_group.setBackgroundResource(R.drawable.circle_grey);
        }
        mHolder.tv_group.setText(group.getName());
        mHolder.tv_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface onGroupHorizontalClickListener {
        void onClick(int position);
    }

    public class groupHorizontalHolder extends RecyclerView.ViewHolder {
        public TextView tv_group;

        public groupHorizontalHolder(View itemView) {
            super(itemView);
            tv_group = (TextView) itemView.findViewById(R.id.tv_grouphorizontal_group);
        }
    }
}
